<?php

namespace App\Home\Requests;

use App\Admin\Models\SystemModel;
use App\Home\Models\WorshipCollection;
use Illuminate\Support\Arr;

class WorshipCollectionRequest extends FormRequest
{
public $validationRules = <<< EOF
{
    "name": "max:50|required",
    "description": "max:255",
    "worship_hymns": "max:65535",
    "pic_url": "max:255",
    "play_count": "max:255",
    "hymn_count": "max:255"
}
EOF;   

    public function rules()
    {
        $worshipCollection = app(WorshipCollection::class);
        $rules = json_decode($this->validationRules, true);
        $id = (int) optional($this->route('worship-collections'))->id;
        $keys = $this->keys();
        if ($this->isMethod('get')) {
            $rules = Arr::only($rules, ['input']);
        }
        if ($this->isMethod('post')) {
            if (in_array('input', $keys)) {
                $rules['input'] .= '|unique:'.$worshipCollection->getTable().',input,'.$id;
            }
        }
        if ($this->isMethod('put')) {
            $rules = Arr::only($rules, $keys);
            if (in_array('input', $keys)) {
                $rules['input'] .= '|unique:'.$worshipCollection->getTable().',input,'.$id;
            }
        }
        return $rules;
    }

    public function messages()
    {
        return [];
    }

    public function attributes()
    {
        $systemModel = app(SystemModel::class);
        $worshipCollection = app(WorshipCollection::class);
        return $systemModel->fieldAttributes($worshipCollection->getTable());
    }
}
